//
//  SISSHouseRegistSuccessViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/10/31.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSHouseRegistSuccessViewController.h"
#import "SISSPassengerCell.h"
#import "SISSJsonRequestNetworkingManager+Resources.h"
#import "SISSJsonRequestNetworkingManager+Registration.h"
#import "SISSRecommendHouseViewController.h"

@interface SISSHouseRegistSuccessViewController ()<SISSPassengerCellDelegate>

@property (strong, nonatomic) NSArray<SISSPassengerResourceModel *> *passengerResourceArr;

@end

@implementation SISSHouseRegistSuccessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"房源登记成功";
    
    self.tipStr = @"以下客源正需要购买同类型房源，您可以点击推荐房源给下面的客户。";
    [self.tableView registerNib:[UINib nibWithNibName:@"SISSPassengerCell" bundle:nil] forCellReuseIdentifier:@"passengerCellReuseID"];
    
//    [self requestData];
}

- (void)backReccommend {
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[SISSRecommendHouseViewController class]]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
}

- (void)setAhrId:(NSString *)ahrId {
    _ahrId = ahrId;
    [self requestData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.passengerResourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    SISSPassengerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"passengerCellReuseID" forIndexPath:indexPath];
    cell.delegate = self;
    
    cell.passengerModel = self.passengerResourceArr[indexPath.row];
    
    return cell;
}

#pragma mark - SISSPassengerCellDelegate
- (void)passengerCellRecommendButtonDidClick:(UIButton *)button {
    
    SISSPassengerCell *cell = (SISSPassengerCell *)button.nextResponder.nextResponder;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    SISSPassengerResourceModel *model = self.passengerResourceArr[indexPath.row];
    SISSHouseResourceModel *houseModel = [[SISSHouseResourceModel alloc] init];
    houseModel.ID = self.ahrId;
    houseModel.houseresType = 2;
    houseModel.isSelected = YES;
    [kJsonRequestNetworkingManager requestInsetRecommendHouseWithPassengerId:model.ID houseArr:@[houseModel] success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 200) {
            NSLog(@"%@",responseObject[@"message"]);
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [SISSTools showSVSuccessTitle:@"推荐成功" andTime:2 andColor:YES];
                [self requestData];
            }];
            
        }
        
    } failed:^(NSError *error) {
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [SISSTools showSVErrorTitle:@"推荐失败。" andTime:2 andColor:YES];
        }];
        NSLog(@"%@",error);
    }];
    
}

- (void)requestData {
    
    [kJsonRequestNetworkingManager requestHouseRecommendDemandWithAhrId:self.ahrId success:^(NSArray<SISSPassengerResourceModel *> *passengerResources) {
        
        self.passengerResourceArr = passengerResources;
        [self.tableView reloadData];
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}



@end
